<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="con"></div>
</body>
<script>
    //通过组件的实例化过程完成组件定义
    //有独立的作用域、构造函数实例化，可以生成新的对象、调用方法可以添加到构造函数的prototype上
    ((w,d) => {
        function Timer(config) {
            this.btn = d.createElement('button');
            this.btn.innerHTML = config.text;
            this.cssStyles = config.cssStyles;
            this.intervalid = '';
            this.count = config.count;
            this.btn.onclick = this.setCount.bind(this);
            this.setStyle();
            config.con.appendChild(this.btn);
        }
        Timer.prototype.setStyle = function() {
            for(let i in this.cssStyles) {
                this.btn.style[i] = this.cssStyles[i]
            }
        }
        Timer.prototype.setCount = function() {
            if(this.intervalid) {
                clearInterval(this.intervalid);
                this.intervalid = '';
            }else{
                this.intervalid = setInterval(() => {
                    this.btn.innerHTML = this.count--;
                },1000)
            }
        }
        w.Timer = Timer;
    })(window,document)

    let timer = new Timer({
        text:'start',
        count:10,
        cssStyles:{
            color:'red'
        },
        con:document.querySelector('.con')
    })
</script>
</html>